package com.bwei.mypaot.menu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.activity.BaseActivity;
import com.bwei.mypaot.base.activity.MainActivity;
import com.bwei.mypaot.base.bean.Loginbean;
import com.bwei.mypaot.utils.Constant;
import com.bwei.mypaot.utils.DESMD5Utils;
import com.bwei.mypaot.utils.OKHttpSingleTop;
import com.squareup.okhttp.Request;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.io.IOException;
import java.util.Map;

import static com.bwei.mypaot.base.activity.MyApplication.oKHttpSingleTop;

/**
 * Created by Administrator on 2017/2/17.
 */

public class LoginActivity extends BaseActivity  {

    private Button login;
    private Button wechat;
    private Button qq;
    private EditText et_loginphone;
    private EditText et_loginpass;
    private String phone;
    private String md5pass;
    private String despass;

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
        }
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
            System.out.println(data.get("screen_name")+","+data.get("profile_image_url")+","+data.get("city")+","+data.get("province")+","+data.get("gender"));

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        initHeader();
        initWidget();
        setWidgetState();
    }

    @Override
    public void initHeader() {
        inittHeaderWidget();
        setTitle("登陆");
        addImageLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        addBtnRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);


            }
        },"注册");


    }

    @Override
    public void initWidget() {
        login = (Button) findViewById(R.id.login);
        wechat = (Button) findViewById(R.id.wechat);
        qq = (Button) findViewById(R.id.qq);
        et_loginphone = (EditText) findViewById(R.id.et_loginphone);
        et_loginpass = (EditText) findViewById(R.id.et_loginpass);
    }

    @Override
    public void setWidgetState() {
        login.setOnClickListener(this);
        wechat.setOnClickListener(this);
        qq.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        String loginphone=et_loginphone.getText().toString().trim();
        String loginpass=et_loginpass.getText().toString().trim();
//电话加密
        DESMD5Utils des=new DESMD5Utils();
        phone = des.encrypt(loginphone);
//密码加密
        md5pass = DESMD5Utils.MD5(loginpass,true);
        despass = des.encrypt(md5pass);
        if (map!=null){
            map.clear();
        }else{
            map.put("phone_num",phone);
            map.put("user_password", despass);
        }

        String url= Constant.WJF_SERVICE+Constant.ServiceConstant.Receive_LOGIN;
        switch (view.getId()){
            case R.id.login:
                 //把url和map集合传给post请求方法
                oKHttpSingleTop.setUrl(url,map, Loginbean.class, OKHttpSingleTop.Methods.POST);
                oKHttpSingleTop.setCallbackM(new OKHttpSingleTop.CallbackM() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Object response) {
                        Toast.makeText(LoginActivity.this,(String) response,Toast.LENGTH_LONG).show();
                        Log.d("KKK", (String) response);
//                        Loginbean loginbean= (Loginbean) response;
//                        if (loginbean.phone_num.equals(map.get("phone_num"))&&loginbean.){
//                            Intent intent=new Intent(LoginActivity.this, MainActivity.class);
//                            startActivity(intent);
//                            finish();
//                        }

                    }
                });



                break;
            case R.id.wechat:

                break;
            case R.id.qq:
                //第三方实训
                UMShareAPI  mShareAPI = UMShareAPI.get( LoginActivity.this );
                mShareAPI.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
               //获取用户信息
                mShareAPI.getPlatformInfo(this, SHARE_MEDIA.QQ, umAuthListener);
                break;

        }


    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }
}
